package com.example.chesspowerup;


import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;

public class PlayGround extends Activity implements OnTouchListener{
	
	ChessSurface ourSurfaceView;
	float x, y;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		ourSurfaceView = new ChessSurface(this);
		ourSurfaceView.setOnTouchListener(this);
		setContentView(ourSurfaceView);
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		ourSurfaceView.pause();
	}


	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		ourSurfaceView.resume();
	}

	@Override
	public boolean onTouch(View arg0, MotionEvent event) {
		// TODO Auto-generated method stub
		x = event.getX();
		y = event.getY();
		return true;
	}
	
	public class ChessSurface extends SurfaceView implements Runnable {

		SurfaceHolder ourHolder;
		Thread ourThread = null;
		boolean isRunning = false;
		Canvas canvas;
		
		int dx, dy, width, height, swidth, sheight, xTwo;
		Paint paint = new Paint();
		int position[][] = new int[8][8];
		
		String white = "white";
		String black = "black";
		
		public ChessSurface(Context context) {
			// TODO Auto-generated constructor stub
			super(context);
			ourHolder = getHolder();
		}
		
		public void pause(){
			isRunning = false;
			while(true){
				try {
					ourThread.join();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				break;
			}
			ourThread = null;
		}
		
		public void resume(){
			isRunning = true;
			ourThread = new Thread(this);
			ourThread.start();
			
		}

		@Override
		public void run() {
			// TODO Auto-generated method stub
			while(isRunning){
				if(!ourHolder.getSurface().isValid())
					continue;
				
				Canvas canvas = ourHolder.lockCanvas();
				canvas.drawRGB(0, 0, 0);
				
				width = height = getWidth()/8;
				dx = dy = 0;
				
				for (int i = 0; i < 8; i++) {
					dx = (i * width);
					position[i][0] = i;
					for (int j = 0; j < 8; j++) {
						dy = (j * height)+64
								;
						position[0][j] = j;
						
						
						if((i % 2)==(j % 2)){
							paint.setColor(Color.GRAY);
						}else{
							paint.setColor(Color.BLACK);
						}
						canvas.drawRect(dx, dy, dx + width, dy + height, paint);
					}				
				}
				for(int r=0; r<position.length; r++) {
					System.out.print("i:"+position[r][0]+ " ");
					//System.out.println();
				       for(int c=0; c<position[r].length; c++){
				    	  // System.out.println();
				           System.out.print("j:"+position[0][c] + " ");
				           System.out.println();
			        }
				}

					
					Bitmap pawnTest = BitmapFactory.decodeResource(getResources(), R.drawable.pawn_white_128);
					canvas.drawBitmap(pawnTest, x, y,null);

				if(x != 0 && y != 0){
					Bitmap test = BitmapFactory.decodeResource(getResources(), R.drawable.king_black_128);
					//canvas.drawBitmap(test, x - (test.getWidth()/2), y - (test.getHeight()/2), null);
					
				}
				ourHolder.unlockCanvasAndPost(canvas);
			}
		}
		
		public void onDraw(Canvas canvas){
			
		}
		
		/*public void initilaize(){
			for(int i = 0; i<8;i++){
				int pos = position[1][i];
				Bitmap pawnTest = BitmapFactory.decodeResource(getResources(), R.drawable.pawn_white_128);
				canvas.drawBitmap(pawnTest, i*64, y+64,null);
			}
		}*/
	}
}
	
	
	
		

	

